﻿using App.Chat.Application.Interface;
using App.Chat.Contract;
using App.Chat.Instructure.IRepositories;
using App.Chat.Models;
using App.Chat.Models.Models;

namespace App.Chat.Application.Implements
{
    public class FriendService : IFriendService
    {
        private readonly IFriendRepository _friendRepository;
        private readonly IUserRepository _userRepository;
        private readonly IFriendAskRepository _friendAskRepository;
        private readonly MongoService _mongoService;
        public FriendService(IFriendRepository friendRepository,
            IUserRepository userRepository,
            IFriendAskRepository friendAskRepository)
        {
            _friendRepository = friendRepository;
            _userRepository = userRepository;
            _friendAskRepository = friendAskRepository;
        }

        public async Task AddFriendAskAsync(FriendAsk ask)
        {
            var isUserExist = await _userRepository.EntityIsExistAsync(q => q.Where("Id", ask.AskUserId));
            var isAskExist = await _friendAskRepository.EntityIsExistAsync(q =>
            q.Where("AskUserId", ask.AskUserId)
            .Where("FromId", ask.FromId)
            .Where("IsAgree", AskState.NOACK));
            var isFriend = await _friendRepository.EntityIsExistAsync(q =>
            q.Where("UserId", ask.FromId).Where("FriendId", ask.AskUserId));
            //查看对方是否已经是朋友
            if (isFriend)
                return;
            if (!isUserExist)
                throw new Exception("用户不存在");
            //如果有相同的记录处于未答复状态不可重复添加
            if (!isAskExist)
            {
                await _friendAskRepository.AddAsync(ask);
                await _mongoService.FriendAskAddTipIncAsync(ask.AskUserId);
            }
        }

        public async Task ChangeRemarkAsync(string currentUserId, string friendId, string friendRemark)
        {
            var friend = await _friendRepository.FindAsync(x => x.UserId.Equals(currentUserId) && x.FriendId.Equals(friendId));
            friend!.FriendRemark = friendRemark;
            //修改mongodb的备注
            await _mongoService.ChangeUserRemarkAsync(currentUserId, friendId, friendRemark);
        }

        public async Task DecideFriendAskAsync(CurrentUser currentUser, string askId, AskState isAgree)
        {
            var ask = await _friendAskRepository.FindByIdAsync(askId);
            if (ask == null)
                throw new Exception("好友请求不存在");
            //TODO 添加修改时间
            ask.IsAgree = isAgree;
            if (isAgree == AskState.AGREE)
            {
                await _friendRepository.AddListAsync(new List<Friend>{
                    new Friend
                    {
                        UserId = ask.FromId,
                        FriendId = ask.AskUserId
                    },
                    new Friend
                    {
                        UserId = ask.AskUserId,
                        FriendId = ask.FromId
                    }});
            }
            await _mongoService.FriendAddSuccessTipInc(ask.FromId);
        }

        public async Task DeleteFriendAsync(string currentUserId, string friendId)
        {
            //单方面删除
            var friend = await _friendRepository.FindAsync(x => x.UserId.Equals(currentUserId) && x.FriendId.Equals(friendId));
            _friendRepository.DeleteSingle(friend);
        }

        public async Task<List<Friend>> FindFriendListAsync(string id)
        {
            return await _friendRepository.EntityQueryAsync(q => q.Where("UserId", id));
        }

        public async Task<List<FriendAsk>> FriendAskListAsync(string id)
        {
            return await _friendAskRepository.EntityQueryAsync(q =>
            q.Where("AskUserId", id)
            .OrderBy("IsAgree")
            .OrderByDesc("CreateTime"));
        }
    }
}
